package com.itheima.sh.test;

import com.itheima.sh.dao.UserMapper;
import com.itheima.sh.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MybatisTest01 {
    private static UserMapper mapper = null;

    @BeforeClass
    public static void beforeClass() throws Exception {
        //1.创建工厂创造类对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //2.获取工厂对象
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sessionFactory = builder.build(is);
        //3.创建会话对象
        SqlSession sqlSession = sessionFactory.openSession(true);
        //4.获取接口的动态代理对象
        mapper = sqlSession.getMapper(UserMapper.class);
    }

    @Test
    public void queryAllUsers() {

        //5.使用接口代理对象调用接口中的方法
        List<User> list = mapper.queryAllUsers();
        System.out.println("list = " + list);
    }
    /*
            【目标】查询id为1的用户及其订单信息
            User queryOneToMany(@Param("id") Long id);
    */
    @Test
    public void queryOneToMany() {
        User user = mapper.queryOneToMany(1L);
        System.out.println("user = " + user);
    }

}
